//
//  ZHPlayerProgressView.m
//  AVPlayer
//
//  Created by 正合适 on 2017/10/28.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "ZHPlayerProgressView.h"
#import "ZHBaseAVPlayerMacro.h"

@interface ZHPlayerProgressView ()
/**总进度条*/
@property (nonatomic, strong) UIView *totalProgressView;
/**缓冲进度条*/
@property (nonatomic, strong) UIView *bufferProgressView;
/**播放时长进度条*/
@property (nonatomic, strong) UIView *playTimeProgressView;
/**游标*/
@property (nonatomic, strong) UIView *indicaterView;
/**拖动起始点*/
@property (nonatomic, assign) CGPoint dragBeginPoint;
@end

@implementation ZHPlayerProgressView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

#pragma mark - 初始化界面
- (void)createUI {
    self.backgroundColor = ZHRGBAColor(0, 0, 0, 0);
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    _totalProgressView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.frame.size.height-5)/2.0, self.frame.size.width, 3)];
    _totalProgressView.layer.cornerRadius = _totalProgressView.frame.size.height/2.0;
    _totalProgressView.backgroundColor = ZHRGBAColor(255,255,255,0.5);
    _totalProgressView.clipsToBounds = YES;
    _totalProgressView.userInteractionEnabled = NO;
    [self addSubview:_totalProgressView];
    _totalProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    _bufferProgressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, _totalProgressView.frame.size.height)];
    _bufferProgressView.layer.cornerRadius = _bufferProgressView.frame.size.height/2.0;
    _bufferProgressView.backgroundColor = ZHRGBAColor(255,255,255,0.8);
    _bufferProgressView.clipsToBounds = YES;
    _bufferProgressView.userInteractionEnabled = NO;
    [_totalProgressView addSubview:_bufferProgressView];
    _bufferProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    _playTimeProgressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, _totalProgressView.frame.size.height)];
    _playTimeProgressView.backgroundColor = ZHRGBAColor(55, 146, 229 ,1);
    _playTimeProgressView.layer.cornerRadius = _playTimeProgressView.frame.size.height/2.0;
    _playTimeProgressView.clipsToBounds = YES;
    _playTimeProgressView.userInteractionEnabled = NO;
    [_totalProgressView addSubview:_playTimeProgressView];
    _playTimeProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    float indicaterW = 15.0;
    _indicaterView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.frame.size.height-indicaterW)/2.0, indicaterW, indicaterW)];
    _indicaterView.userInteractionEnabled = YES;
    _indicaterView.backgroundColor = [UIColor whiteColor];
    _indicaterView.layer.cornerRadius = indicaterW/2.0;
    [self addSubview:_indicaterView];
    _indicaterView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    float viewW = _indicaterView.frame.size.width*0.4;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((_indicaterView.frame.size.width-viewW)/2.0, (_indicaterView.frame.size.width-viewW)/2.0, viewW, viewW)];
    view.backgroundColor = ZHRGBAColor(55, 146, 229 ,1);
    view.clipsToBounds = YES;
    view.layer.cornerRadius = viewW/2.0;
    [_indicaterView addSubview:view];
    
}

#pragma mark - 设置当前的播放进度
- (void)configPlayProgress:(float)bfb {
    float width = bfb * (self.frame.size.width-_indicaterView.frame.size.width);
    _indicaterView.frame = CGRectMake(width, _indicaterView.frame.origin.y, _indicaterView.frame.size.width, _indicaterView.frame.size.height);
    _playTimeProgressView.frame = CGRectMake(_playTimeProgressView.frame.origin.x, _playTimeProgressView.frame.origin.y, _indicaterView.center.x, _playTimeProgressView.frame.size.height);
}

#pragma mark - 设置缓冲进度条
- (void)configBufferProgress:(float)bfb {
    float width = bfb * self.frame.size.width;
    _bufferProgressView.frame = CGRectMake(_bufferProgressView.frame.origin.x, _bufferProgressView.frame.origin.y, width, _bufferProgressView.frame.size.height);
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:_indicaterView];
    _dragBeginPoint = point;
    [self callbackWIthStatus:0];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:_indicaterView];
    float dx = point.x - _dragBeginPoint.x;
    CGPoint newcenter = CGPointMake(_indicaterView.center.x + dx, _indicaterView.center.y);
    float halfx = CGRectGetMidX(_indicaterView.bounds);
    newcenter.x = MAX(halfx, newcenter.x);
    newcenter.x = MIN(_indicaterView.superview.bounds.size.width - halfx, newcenter.x);
    _indicaterView.center = newcenter;
    _playTimeProgressView.frame = CGRectMake(_playTimeProgressView.frame.origin.x, _playTimeProgressView.frame.origin.y, _indicaterView.center.x, _playTimeProgressView.frame.size.height);
    [self callbackWIthStatus:1];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self callbackWIthStatus:2];
}

//#pragma mark - 回调 0:开始 1：拖动中 2：拖动结束
- (void)callbackWIthStatus:(NSInteger)status {
    float bfb = _indicaterView.frame.origin.x/(self.frame.size.width-_indicaterView.frame.size.width);
    if (self.dragBlcok) {
        self.dragBlcok(status, bfb);
    }
}

@end
